import O1HD, { managers } from "../../../../engines/gameEngine";
import DragBase from "../../baseScript/Drag";
import levelFindSprieBase from "./levelFindSpriteBase";

const { ccclass, property } = cc._decorator;

@ccclass
export default class levelFindSpriteAnswerNode extends DragBase {

    @property({ type: levelFindSprieBase, displayName: "关卡脚本" })
    levelScript: levelFindSprieBase = null;

    nameNode: cc.Node = null;

    circleNode: cc.Node = null;

    @property({ displayName: "是否是答案节点" })
    isAnswer: boolean = false;

    @property({ displayName: "需要放大多少倍才能被点击" })
    minMultiple: number = 0.9;
    @property({ displayName: "是否需要点击放大" })
    isneedScale: boolean = true

    @property()
    sound: string = "";

    onLoad() {
        super.onLoad();
        let answer: cc.Node = this.node.getChildByName("answer");
        this.nameNode = answer.getChildByName("nameFrame");
        this.circleNode = answer.getChildByName("quan");
        this.init();
    }

    init() {
        this.circleNode.active = false;
        this.nameNode.active = false;
    }

    update(dt) {
        if (this.startCount) {
            this.startTouchTime += dt;
            if (this.startTouchTime >= 1) {
                this.startTouchTime = 0;
                if (this.sound != "") {
                    O1HD.getManagers().soundMgr.playSound(this.sound);
                }
                if (this.isOk) {
                    return;
                }
                this.levelScript.currentOkNum++;
                this.isOk = true;
                managers().soundMgr.playSound('mainSound/hcy1'); // 正确音效
                managers().soundMgr.playVibrate();
                // 显示答案节点
                this.levelScript.addOneAnswer(this.tagNum);
                // let daanString: string = "daan_" + this.tagNum;
                // this.levelScript.DaAnNode.getChildByName(daanString).active = true;
                // this.node.getChildByName("daan_quan").active = true;
                this.circleNode.active = true;
                this.nameNode.active = true;
                this.scheduleOnce(() => {
                    this.nameNode.active = false;
                }, 2);
                this.startCount = false;
                console.log("数量：",this.levelScript.currentOkNum,"需求数量：", this.levelScript.needCount)
                if (this.levelScript.currentOkNum == this.levelScript.needCount) {
                    this.levelScript.isPlaySound = true;
                    this.levelScript.winAni().then(() => {
                        this.levelScript.Win(2);
                    });
                }
            }
        }
    }
    /** 对应标签 */
    @property({ type: cc.Integer, displayName: "对应标签" })
    tagNum: number = 0;

    /** 是否已经回答 */
    isOk: boolean = false;

    startCount: boolean = false;
    startTouchTime: number = 0;
    protected startTouchPos: cc.Vec2 = cc.Vec2.ZERO;
    moveComponentStart(touchEvent: cc.Event.EventTouch): void {
        if (!this.isAnswer) return;
        if (this.levelScript.AMPNode.scale < this.minMultiple) return;
        managers().soundMgr.playVibrate();
        touchEvent.stopPropagation();
        if (this.isneedScale) this.node.scale = 1.2;
        this.startCount = true;
        if (this.levelScript.isPlaySound) return;
        if (this.isOk) return;
        if (!this.isAnswer) return;
        this.startTouchPos = touchEvent.getLocation();
        let nextPos: cc.Vec2 = this.node.parent.convertToNodeSpaceAR(this.startTouchPos);
        /** 跟随移动 */
        // this.node.setPosition(cc.v2(nextPos.x, nextPos.y));
    }

    moveComponent(touchEvent: cc.Event.EventTouch): void {
        // touchEvent.stopPropagation();
        // if (this.levelScript.isPlaySound) return;
        // if (this.isOk) return;
        // let touches = touchEvent.getTouches();
        // let touchPos: cc.Vec2 = touchEvent.getLocation();
        // let distance = Math.sqrt((touchPos.y - this.startTouchPos.y) * (touchPos.y - this.startTouchPos.y) + (touchPos.x - this.startTouchPos.x) * (touchPos.x - this.startTouchPos.x));
        // let nextPos: cc.Vec2 = this.node.parent.convertToNodeSpaceAR(touchPos);

        // if (this.startTouchTime > 1) {
        //     this.levelScript.currentOkNum++;
        //     let daanString: string = "daan_" + this.tagNum;
        //     this.levelScript.DaAnNode.getChildByName(daanString).active = true;
        //     this.levelScript.DaAnNode.getChildByName(daanString).getChildByName("daan_quan").active = true;
        // }

        /** 跟随移动 */
        // this.node.setPosition(cc.v2(nextPos.x, nextPos.y));

    }
    moveComponentEnd(touchEvent: cc.Event.EventTouch): void {
        if (this.isneedScale) this.node.scale = 1;
        touchEvent.stopPropagation();
        this.startCount = false;
        this.startTouchTime = 0;
        if (this.levelScript.isPlaySound) return;
        if (this.isOk) return;
        if (!this.isAnswer) return;
        let touchPos: cc.Vec2 = touchEvent.getLocation();

        let nextPos: cc.Vec2 = this.node.parent.convertToNodeSpaceAR(touchPos);
        let distance = Math.sqrt((touchPos.y - this.startTouchPos.y) * (touchPos.y - this.startTouchPos.y) + (touchPos.x - this.startTouchPos.x) * (touchPos.x - this.startTouchPos.x));



    }

    

}
